﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using BackgroundQueueServiceSample.Model;
using BlueDot.Data.Core;
using BlueDot.Data.Core.Sql;
using BlueDot.Mobile.Core;

namespace BackgroundQueueServiceSample.Controllers
{
    public class MainController : ControllerBase
    {
        #region public

        public static void SetupDatabase()
        {
            ProviderFactory.AddProvider(new SqlCeProvider(), true);
            ProviderFactory.DefaultProvider.ConnectionString = String.Format(
                "Data Source = {0}\\BackgroundQueueService.sdf",
                Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().GetName
                        ().CodeBase));

            if (!((SqlCeProvider) ProviderFactory.DefaultProvider).DataStoreExists())
            {
                DataStoreSchemaBuilder.CreateDataStore(new List<Type>
                                                           {
                                                               typeof (Person)
                                                           },
                                                       true);

                LoadSampleData();
            }
        }

        #endregion

        #region private

        private static void LoadSampleData()
        {
            const int num = 500;

            var people = new List<Person>(num);
            for (int i = 0; i < num; i++)
            {
                people.Add(new Person
                               {
                                   FirstName = "Joe " + i,
                                   LastName = "Smith",
                                   Address = "1900 Grant St.",
                                   Email = "jsmith@tst.com",
                               });
            }

            Trace.WriteLine("Creating sample data...");

            DataPersistence.Save(people);
        }

        #endregion
    }
}